package main.sevices;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import main.entities.Provider;
import main.enums.JsonResponseStatus;
import main.interfaces.ProviderBusinessLocal;
import main.models.JsonResponseModel;

@ManagedBean("ProviderServices")
@SessionScoped
@Path("/providerservice")
@Produces({"application/json"})
@Consumes({"application/json"})
public class ProviderServices implements Serializable {

	/**
	 * GUID for Serializable.
	 */
	private static final long serialVersionUID = 4740344205486225701L;
	
	private List<Provider> providers = null;
	
	@EJB(name = "ProviderBusiness")
	private ProviderBusinessLocal _providerEjb;
	
	public ProviderServices() {
		
	}
	
	@PostConstruct
	public void init() {
		providers = _providerEjb.getAllProviders();
	}
	
	@GET
    @Path("/providers")
    public JsonResponseModel getProviders() {
		providers = _providerEjb.getAllProviders();
		return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", providers);
	}
	
    @POST
    @Path("/provider/save")
    public JsonResponseModel saveProvider(Provider provider) {
        int index = -1;
        
        try {
        	provider = _providerEjb.saveProvider(provider);
        }
        catch(Exception e) {
        	return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        }
        
        for(int i = 0; i < providers.size(); i++) {
        	if(providers.get(i).getId().equals(provider.getId())) {
        		index = i;
        		break;
        	}
        }
        
        if(index > -1) {
        	providers.set(index, provider);
        }
        else {
        	providers.add(provider);
        }
        
        System.out.println("Provider [" + provider.getfName() + " " + provider.getlName() + "] was saved.");
        return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", null);
    }
	
    @PUT
    @Path("/provider/add")
    public JsonResponseModel addProvider(Provider provider) {
        try {
        	_providerEjb.addProvider(provider);
        	provider = _providerEjb.getProvider(provider.getId());
        }
        catch(Exception e) {
        	return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        }
        
        providers.add(provider);
        
        System.out.println("New provider [" + provider.getfName() + " " + provider.getlName() + "] created.");
        return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", provider);
    }
    
	@DELETE
	@Path("/provider/delete/{id}")
	public JsonResponseModel deleteProvider(@PathParam("id") Long id) {
		Provider foundProvider = null;
		boolean deleteSuccess = false;
        Iterator<Provider> providerIter = providers.iterator();
        
        while(providerIter.hasNext()) {
            foundProvider = providerIter.next();
            if(foundProvider.getId() == id.intValue()) {
                providerIter.remove();
                break;
            }
            else {
                foundProvider = null;
            }
        }
        
        if(foundProvider != null) {
        	try {
        		deleteSuccess = _providerEjb.deleteProvider(foundProvider);
        	}
        	catch(Exception e) {
        		return new JsonResponseModel(JsonResponseStatus.EXCEPTION, e.getMessage(), null);
        	}
        	
        	if(deleteSuccess) {
	            System.out.println("Provider [" + foundProvider.getfName() + " " + foundProvider.getlName() + "] was deleted.");
	            return new JsonResponseModel(JsonResponseStatus.SUCCESS, "", null);
        	}
        	else {
        		System.out.println("An exception occurred while trying to delete Provider [" + foundProvider.getfName() + " " + foundProvider.getlName() + "].");
	            return new JsonResponseModel(JsonResponseStatus.EXCEPTION, "An exception occurred while trying to delete Provider [" + foundProvider.getfName() + " " + foundProvider.getlName() + "]. Please notify the System Administrator.", null);
        	}
        }
        else {
            System.out.println("Provider not found for id [" + id + "].");
            return new JsonResponseModel(JsonResponseStatus.INVALID, "No Provider could be found for ID [" + id + "].", null);
        }
	}
	
}
